package com.wswy.wyjk.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import com.raft.framework.utils.UiUtils
import com.wswy.wyjk.R
import com.wswy.wyjk.utils.invisible
import com.wswy.wyjk.utils.visible
import kotlinx.android.synthetic.main.layout_tips_choose.view.*

class TipsChooserLayout : FrameLayout {

    private var currSelectedView:View?=null

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {


        View.inflate(context, R.layout.layout_tips_choose,this)

        tv_order_practice.isSelected = true

        currSelectedView = tv_order_practice

        //答题模式
        tv_order_practice.setOnClickListener(::updateView)
        //背题模式
        tv_answer_practice.setOnClickListener(::updateView)


        UiUtils.addOnGlobalLayoutListener(view_indicator){
            val x =tv_order_practice.left + (tv_order_practice.right-tv_order_practice.left - view_indicator.width)/2f
            view_indicator.x = x

            view_indicator.visible()
        }

        view_indicator.invisible()
    }

    private fun updateView(view: View){
        if (currSelectedView != view){
            currSelectedView?.isSelected = false
            currSelectedView = view
            currSelectedView?.isSelected = true

            scrollToView(view)

            chooserCallback?.invoke( if(view == tv_order_practice) 0 else 1 )

        }
    }

    private fun scrollToView(view: View){
        val x =view.left + (view.right-view.left - view_indicator.width)/2f

        view_indicator.animate().x(x).start()

    }

    var  chooserCallback : ((Int)->Unit?)?=null

}